/**
 * @(#)TestChannel2.java, Nov 21, 2012. 
 * 
 * Copyright 2012 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.buaaer.net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 *
 * @author wangheng
 *
 */
public class TestChannel2 {
    
    
    public static void main(String[] args) throws Exception {
        
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        ServerSocket socket = serverChannel.socket();
        socket.bind(new InetSocketAddress(12345));
        
        serverChannel.configureBlocking(false);//
        
        Selector selector = Selector.open();
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        while(true) {
            System.out.println("before");
            selector.select();
            System.out.println("end");
            
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            System.out.println("size:" + selector.selectedKeys().size());
            
            int flag = 0;
            while (iterator.hasNext()) {
                System.out.println("flag:" + (++flag));
                SelectionKey key = iterator.next();
                iterator.remove();
                
                if (key.isAcceptable()) {
                    System.out.println("aaaaa");
                    ServerSocketChannel server = (ServerSocketChannel)key.channel();
                    SocketChannel client = server.accept();
                    System.out.println("accept:" + client);
                    
                    client.configureBlocking(false);//
                    SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
                    ByteBuffer buffer = ByteBuffer.allocate(100);
                    buffer.put("hello world2!!!".getBytes());
                    buffer.flip();
                    key2.attach(buffer);
                    
                } else if (key.isWritable()) {
                   System.out.println("bbbbb");
                   SocketChannel client = (SocketChannel)key.channel();
                   ByteBuffer buffer = (ByteBuffer)key.attachment();
                   buffer.put("hello world!!!".getBytes());
                   buffer.flip();
                   
                   key.attach(buffer);
                   client.write(buffer);
                   client.close();
                } 
            }
            
            
        }
        
        
    }
    
    

    /**
     * @param args
     * @throws IOException 
     */
    public static void main1(String[] args) throws IOException {
        
        ServerSocket server = new ServerSocket(12345);
        
        Socket socket = server.accept();
        
        OutputStream out = socket.getOutputStream();
        
        byte[] b = "hello world".getBytes();
        out.write(b);
        out.flush();
        socket.close();
        
    }

}
